from typing import List
from fastapi import APIRouter, Depends, HTTPException, status
from app.schemas.moment import Moment, MomentCreate, MomentUpdate, Like, LikeCreate, Comment, CommentCreate, CommentUpdate

router = APIRouter()

@router.post("/", response_model=Moment)
async def create_moment(moment: MomentCreate):
    """发布动态"""
    # 这里应该从认证信息中获取用户ID
    # 为简化示例，我们直接返回一个模拟动态
    return Moment(
        id="mock_moment_id",
        user_id="mock_user_id",
        content=moment.content,
        images=moment.images or [],
        is_public=moment.is_public,
        like_count=0,
        comment_count=0,
        created_at="2023-01-01T00:00:00",
        updated_at="2023-01-01T00:00:00"
    )

@router.get("/", response_model=List[Moment])
async def read_moments(skip: int = 0, limit: int = 100):
    """获取朋友圈动态"""
    # 这里应该根据用户关系获取朋友圈动态
    # 为简化示例，我们直接返回一个模拟动态列表
    return [
        Moment(
            id="mock_moment_id_1",
            user_id="mock_user_id_1",
            content="这是第一条动态",
            images=[],
            is_public=True,
            like_count=10,
            comment_count=5,
            created_at="2023-01-01T00:00:00",
            updated_at="2023-01-01T00:00:00"
        ),
        Moment(
            id="mock_moment_id_2",
            user_id="mock_user_id_2",
            content="这是第二条动态",
            images=["image_url_1", "image_url_2"],
            is_public=True,
            like_count=20,
            comment_count=5,
            created_at="2023-01-01T00:00:00",
            updated_at="2023-01-01T00:00:00"
        )
    ]

@router.put("/{moment_id}", response_model=Moment)
async def update_moment(moment_id: str, moment: MomentUpdate):
    """更新动态"""
    # 验证动态是否存在且属于当前用户
    # 更新动态内容
    
    # 为简化示例，我们直接返回一个模拟动态
    updated_moment = Moment(
        id=moment_id,
        user_id="mock_user_id",
        content=moment.content or "更新后的动态内容",
        images=moment.images or [],
        is_public=moment.is_public if moment.is_public is not None else True,
        like_count=0,
        comment_count=0,
        created_at="2023-01-01T00:00:00",
        updated_at="2023-01-01T00:00:00"
    )
    
    return updated_moment

@router.delete("/{moment_id}")
async def delete_moment(moment_id: str):
    """删除动态"""
    # 验证动态是否存在且属于当前用户
    # 删除动态及相关评论、点赞
    
    return {"message": "Moment deleted successfully"}

@router.post("/{moment_id}/like", response_model=Like)
async def like_moment(moment_id: str, like: LikeCreate):
    """点赞动态"""
    # 验证动态是否存在
    # 检查用户是否已点赞
    # 创建点赞记录
    # 更新动态点赞数
    
    # 为简化示例，我们直接返回一个模拟点赞
    return Like(
        id="mock_like_id",
        user_id=like.user_id,
        moment_id=moment_id,
        created_at="2023-01-01T00:00:00"
    )

@router.post("/{moment_id}/comments", response_model=Comment)
async def comment_moment(moment_id: str, comment: CommentCreate):
    """评论动态"""
    # 验证动态是否存在
    # 创建评论记录
    # 更新动态评论数
    
    # 为简化示例，我们直接返回一个模拟评论
    return Comment(
        id="mock_comment_id",
        user_id=comment.user_id,
        moment_id=moment_id,
        content=comment.content,
        parent_comment_id=comment.parent_comment_id,
        created_at="2023-01-01T00:00:00"
    )

@router.put("/{moment_id}/comments/{comment_id}", response_model=Comment)
async def update_comment(moment_id: str, comment_id: str, comment: CommentUpdate):
    """更新评论"""
    # 验证评论是否存在且属于当前用户
    # 更新评论内容
    
    # 为简化示例，我们直接返回一个模拟评论
    return Comment(
        id=comment_id,
        user_id="mock_user_id",
        moment_id=moment_id,
        content=comment.content or "更新后的评论内容",
        parent_comment_id=None,
        created_at="2023-01-01T00:00:00"
    )

@router.delete("/{moment_id}/comments/{comment_id}")
async def delete_comment(moment_id: str, comment_id: str):
    """删除评论"""
    # 验证评论是否存在且属于当前用户
    # 删除评论
    # 更新动态评论数
    
    return {"message": "Comment deleted successfully"}